package com.devin.service.config;

import netscape.javascript.JSUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component("myAuthenticationFailuerHandler")
public class MyAuthenticationFailuerHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        System.out.println("==============fail----FailureHandler=========");
//        httpServletResponse.sendRedirect("/fail");
        /*认证失败处理，一般登陆页认证直接用表单验证解决了，不会在此处解决，
          因此模拟一个状态码，使其跳转到/error下
        */
        httpServletRequest.setAttribute("javax.servlet.error.status_code",666);
        httpServletRequest.getRequestDispatcher("/error").forward(httpServletRequest,httpServletResponse);
    }
}
